package org.dream.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


/**
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class ReaderExcel {
	/**
	 * @return 返回String[] 的列表
	 */
	public static List<String[]> readExcel(String fileName) {
		return readExcel(new File(fileName));
	}

	/**
	 * @return 返回String[] 的列表
	 */
	public static List<String[]> readExcel(File file) {
		try {
			return readExcel(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * @return 返回String[] 的列表(只读取第一个sheet页)
	 */
	public static List<String[]> readExcelOneSheet(File file) {
		try {
			return readExcelOneSheet(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * @return 返回String[] 的列表
	 */
	public static List<String[]> readExcel(InputStream in) {
		List<String[]> lt = new ArrayList<String[]>();
		Workbook wb = null;

		try {
			wb = Workbook.getWorkbook(in);
			Sheet[] sheets = wb.getSheets(); // 获取工作
			for (int i = 0; i < sheets.length; i++) {
				Sheet sheet = sheets[i];
				for (int j = 0; j < sheet.getRows(); j++) {
					Cell[] cells = sheet.getRow(j); // 读取一行
					if (cells != null && cells.length > 0) { // 这一行有内容才添加
						String[] dataCells = new String[cells.length];
						for (int k = 0; k < cells.length; k++) {
							dataCells[k] = "" + cells[k].getContents(); // 读内容
						}// column
						lt.add(dataCells);
					}
				}// one sheet
			}// xls file
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (wb != null) {
				wb.close();
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

		return lt;
	}

	public static List<String[]> readExcelByFile(File file) {
		List<String[]> lt = new ArrayList<String[]>();
		Workbook wb = null;

		try {
			wb = Workbook.getWorkbook(file);
			Sheet[] sheets = wb.getSheets(); // 获取工作
			for (int i = 0; i < sheets.length; i++) {
				Sheet sheet = sheets[i];
				for (int j = 0; j < sheet.getRows(); j++) {
					Cell[] cells = sheet.getRow(j); // 读取一行
					if (cells != null && cells.length > 0) { // 这一行有内容才添加
						String[] dataCells = new String[cells.length];
						for (int k = 0; k < cells.length; k++) {
							dataCells[k] = "" + cells[k].getContents(); // 读内容
						}// column
						lt.add(dataCells);
					}
				}// one sheet
			}// xls file
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (wb != null) {
				wb.close();
			}
		}

		return lt;
	}

	

	/**
	 * @return 返回String[] 的列表(只读取第一个sheet页)
	 */
	public static List<String[]> readExcelOneSheet(InputStream in) {
		List<String[]> lt = new ArrayList<String[]>();
		Workbook wb = null;
		try {
			wb = Workbook.getWorkbook(in);
			Sheet[] sheets = wb.getSheets(); // 获取工作
			Sheet sheet = sheets[0];
			for (int j = 0; j < sheet.getRows(); j++) {
				Cell[] cells = sheet.getRow(j); // 读取一行
				if (cells != null && cells.length > 0) { // 这一行有内容才添加
					String[] dataCells = new String[cells.length];
					for (int k = 0; k < cells.length; k++) {
						dataCells[k] = "" + cells[k].getContents(); // 读内容
					}// column
					lt.add(dataCells);
				}
			}// one sheet
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (wb != null) {
				wb.close();
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return lt;
	}
}
